// VBConversions Note: VB project level imports
using System.Collections.Generic;
using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Xml.Linq;
using Microsoft.VisualBasic.Compatibility;
using System.Windows.Forms;
using System.Collections;
using System.Data;
// End of VB project level imports

using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Net;
using System.Management;
using System.Management.Instrumentation;
using System.Drawing.Drawing2D;
using Microsoft.Win32;
using Microsoft.VisualBasic.CompilerServices;

namespace 喷码数据生成工具
{
	sealed class CommFunction
	{
		//用于获取自windows启动以来经历的时间长度（毫秒）
		[DllImport("kernel32", EntryPoint = "GetTickCount", SetLastError = false, 
		CharSet = CharSet.Auto, ExactSpelling = false, 
		CallingConvention = CallingConvention.StdCall)]public static  extern int GetTickCount();
		[Serializable()]public enum EnumCodeMode
		{
			None = 0,
			VBScript = 1,
			JavaScript = 2
		}
		[Serializable()]public struct frmTextEditData
		{
			public int frmX;
			public int frmY;
			public int frmWidth;
			public int frmHeight;
			public System.Windows.Forms.FormWindowState formWindowState;
			public bool isShowStatus;
			public bool isWordWrap;
			public Font TextFont;
			public Color TextColor;
		}
		[Serializable()]public struct CheckMode
		{
			/// <summary>
			/// 校验码函数名
			/// </summary>
			/// <remarks></remarks>
			public string CheckFunName;
			/// <summary>
			/// 校验码名称
			/// </summary>
			/// <remarks></remarks>
			public string CheckName;
			/// <summary>
			/// 校验码位数
			/// </summary>
			/// <remarks></remarks>
			public int CheckLength;
			/// <summary>
			/// 校验码算法VBS源码
			/// </summary>
			/// <remarks></remarks>
			public string CheckCode;
			/// <summary>
			/// 校验方式说明
			/// </summary>
			/// <remarks></remarks>
			public string CheckDescription;
			public EnumCodeMode CodeMode;
		}
		[Serializable()]public struct TabSaveData
		{
			public int StartNumberSelected;
			public string StartNumberText;
			public string FinishNumberText;
			public string StepString;
			public string LengthText;
			public int CheckModeSelected;
			public string Suffix;
			public string Prefix;
			public string InsertChar;
			public ArrayList InsertPosition;
			public ArrayList InsertCharAtPostion;
			public bool Fixed;
			public bool BH;
			public object 扩展信息1;
			public object 扩展信息2;
		}
		[Serializable()]public struct SaveSetting
		{
			public int TabCount;
			public string SaveName;
			public List<TabSaveData> Data;
			public bool 是否加黑线;
			public int 黑线位置;
			public bool 是否补号;
			public int 补号每行号码个数;
			public int 每组行数;
		}
		[Serializable()]public struct SearchData
		{
			/// <summary>
			/// 表名
			/// </summary>
			/// <remarks></remarks>
			public string TableName;
			/// <summary>
			/// 数据库名
			/// </summary>
			/// <remarks></remarks>
			public string DataBaseName;
			/// <summary>
			/// 搜索数据sql语句
			/// </summary>
			/// <remarks></remarks>
			public string SearchSql;
			/// <summary>
			/// 加载数据到datatable的sql语句
			/// </summary>
			/// <remarks></remarks>
			public string DownLoadDataSql;
			/// <summary>
			/// 填充主界面DataGridView数据表的VBS代码
			/// </summary>
			/// <remarks></remarks>
			public string LoadDataVBS;
			/// <summary>
			/// 加载显示描述Sql语句
			/// </summary>
			/// <remarks></remarks>
			public string DescriptionSql;
		}
		
		[Serializable()]public struct Save_DataStructure
		{
			public string LastSaveFilePath;
			public string DataBaseIP;
			public int DataBasePort;
			public string DataBaseUserName;
			public string DataBasePassword;
			public string DataBase;
			public List<SearchData> SearchTable;
			public string LastSearchTable;
			public List<CheckMode> CheckNumberMode;
			public List<SaveSetting> Setting;
			public string RegisterCode;
			public frmTextEditData EditFormData;
			public DHLDataStructure DHLData;
		}
		/// <summary>
		/// DHL箱号标签数据
		/// </summary>
		/// <remarks></remarks>
		///
		[Serializable()]public struct DHLDataStructure
		{
			public string Region;
			public string Country;
			public string RegionCoord;
			public string Product;
			public string Period;
			public string PO_NO;
			public string TO_NO;
			public int 每箱号码数;
			public int 每盘总箱数;
			public string StartNO;
			public string EndNo;
			public long StartXH;
			public long StartPH;
		}
		public static List<CheckMode> CheckData = new List<CheckMode>();
		public static Save_DataStructure SaveData = new Save_DataStructure();
		public static SqlDataBase Sql = new SqlDataBase();
		public static DataTable SearchResult;
		public static SearchData CurrentSearchData;
		public static MSScriptControl.ScriptControl CheckScript = new MSScriptControl.ScriptControl();
		public static bool RegionAccess = false;
		public static float frmSettingX;
		public static float frmSettingY;
		public static MenuSkin MenuSkinRender = new MenuSkin();
		public static List<frm_TextEdit> SaveEditeHandle = new List<frm_TextEdit>();
		public static void LoadCheckMode() //加载校验方式
		{
			CheckData.Clear();
			AddCheckMode("无", "", 0, "", "无校验方式");
			AddCheckMode("模七", "Mod7", 1, "", "模七校验方法：被校验数据除以7的余数。");
			AddCheckMode("模10", "getMod10", 1, "", "1、奇数位之和乘以3加上偶数位之和除以10的余数；" + "\r\n" + "2、当余数为0时则校验码为0；" + "\r\n" + "3.当余数不为0时校验码为10-余数；");
			AddCheckMode("模11", "ModEleven", 1, "", "模十一校验方法： " + "\r\n" + "1、只能对8位号码进行校验；" 
				+ "\r\n" + "2、各位数据对应乘以86423597得到的和除以11的余数;" 
				+ "\r\n" + "3、当余数为0时校验码为5，当余数为1时，校验码为0，余数其它校码为11-余数。");
			AddCheckMode("全一", "QuanYI", 1, "");
			AddCheckMode("顺丰", "SunFeng", 1, "");
			AddCheckMode("九福州", "getAlemtat", 1, "", "Alemtat(九福洲)校验规则：" + "\r\n" + "  需要校验的号码长度为7位，分别取出各位数Str1,Str2,Str3,Str4,Str5,Str6,Str7；再分别乘以8, 4, 5, 7, 9, 2, 1得到乘积，Sr1,Sr2······Sr6,Sr7,再将所得结果求和得到Sum.Sum的个位即为校验码。");
			AddCheckMode("信丰1位", "XinFeng1245", 1, "");
			AddCheckMode("信丰3位", "XinFeng3", 3, "");
			AddCheckMode("百世物流", "BaiShiTong3", 1, "");
			AddCheckMode("港中能达", "GangZhong", 1, "");
			AddCheckMode("Only_Vector", "Only_Vector", 1, "");
			AddCheckMode("WaybillNoList", "getWaybillNoList", 1, "");
			AddCheckMode("港中能达（无限制）", "getGangzhongNengDa", 1, "");
			AddCheckMode("晋越", "GetJinYue", 1, "", "前十位数之和减去第十一位数，所得结果除以9的余数即为校验码。");
			AddCheckMode("中铁", "getCheckZhongtian", 1, "", "中铁校验方式：" + "\r\n" + "  1.代码位置序号" + "\r\n" + "  代码位置序号是指不包括校验码在内的，由右至左的顺序号（不含校验码）。" + "\r\n" + "  2.计算步骤" 
				+ "\r\n" + "  a.从代码位置序号2开始，所有偶数位的数字代码求和。" + "\r\n" + "  b.将步骤a的和乘以3，再加上18。" + "\r\n" + "  c.从代码位置序号3开始，所有奇数的数字代码求和，再乘以2，再减去3." 
				+ "\r\n" + "  d.将步骤b与c的结果相加，再乘以7。" + "\r\n" + "  e.用大于或等于步骤d所得结果且位13最小整数倍的数减去步骤d所得结果，其差的个位即为所求校验码的值。");
			
			AddCheckMode("黄马甲", "Gethuangmajia", 2, "", "号码总长度12位，前十位为流水号，后两位为检验位校验码计算方式:" 
				+ "\r\n" + "    1.分别取前五位数, 得到str1, str2, str3, str4, str5, 再将得到的结果求和得到sum1" 
				+ "\r\n" + "    2.从第六位数开始去五位数, 得到str6, str7, str8, str9, str10, 将得到结果求和得到sum2" 
				+ "\r\n" + "    3.分别将sum1和sum2除以7取余数得到结果, Q1, Q2。" 
				+ "\r\n" + "    4.将Q1和Q2以字符形式连接即是校验码");
			AddCheckMode("深圳石化", "getSHCheck", 2, "");
			AddCheckMode("城市应急", "CityExpress", 1, "", "前十二位流水号，各个位数对应乘以2,4,3,6,7,4,4,9,4,6,5,4，所得结果的和的个位数，X即为校验码");
			AddCheckMode("三菱空调", "getSanlinkongtiao", 1, "", "流水号每位字符对应的Ascii码的和除以10的余数");
			AddCheckMode("安能运单", "GetAnNeng", 1, "", "校验规则：单号为 12位,去掉前缀 2位 ,去掉最后一校验 码，用剩余9位数字Str1参与校验码运算。" + "\r\n" 
				+ "  再用Str1各位数分别乘以7,4,2,5,3,6,8,2,9,再将所得的结果求和得到Sum。" + "\r\n" + "  用得到的和Sum除以11取余数Result。" + "\r\n" + "  当Result等于0时校验码为5；当Result等于1时校验码为0；当Result等于1时校验码为1否则校验码为11-Result。");
			AddCheckMode("三菱重工", "getSanLinZhongGong", 1, "", "取前9位数字之和的个位数+2，此结果取个位数即为检验码。");
			AddCheckMode("深圳交通管理处罚书", "GetGongAnpenalty", 1, "", EnumCodeMode.None);
			AddCheckMode("亚风物流7位", "GetYaFeng7", 1, "");
			AddCheckMode("亚风物流10位", "GetYaFeng10", 1, "");
			AddCheckMode("亚风物流11位", "GetYaFeng11", 1, "");
			AddCheckMode("亚风物流12位", "GetYaFeng12", 1, "");
			AddCheckMode("沙特联邦", "GetShatelianbang", 1, "");
			AddCheckMode("优速V单", "GetYouShuY", 2, "", "优速Y单号码流水号长度为11为，校验码2位：" + "\r\n" 
				+ "1.取流水号第9-12位号码除7取余+3。（例：668107533142，3142 Mode 7 +3=9）" + "\r\n" 
				+ "2.取流水号第11位乘7+3取个位。（例：4*7+3=31，结果为1）" + "\r\n" + "校验码为【91】。");
			CheckScript.Reset();
			CheckScript.Language = "VBScript";
			try //从本地文件加载脚本源码
			{
				if (SaveData.CheckNumberMode != null)
				{
					for (var i = 0; i <= SaveData.CheckNumberMode.Count - 1; i++)
					{
						try
						{
							//MsgBox(SaveData.CheckNumberMode.Item(i).CheckName)
							if (CheckScript.Modules.Count <= 0)
							{
								object null_object = null;
								CheckScript.Modules.Add("CheckMode", ref null_object);
							}
							CheckScript.Language = System.Convert.ToString(SaveData.CheckNumberMode[i].CodeMode.ToString());
							CheckScript.AddCode(System.Convert.ToString(SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckCode));
							AddCheckMode(System.Convert.ToString(SaveData.CheckNumberMode[i].CheckName), System.Convert.ToString(SaveData.CheckNumberMode[i].CheckFunName), System.Convert.ToInt32(SaveData.CheckNumberMode[i].CheckLength), System.Convert.ToString(SaveData.CheckNumberMode[i].CheckCode), System.Convert.ToString(SaveData.CheckNumberMode[i].CheckDescription), SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CodeMode);
						}
						catch (Exception)
						{
							// MsgBox(ex.Message)
						}
					}
				}
			}
			catch (Exception ex)
			{
				Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "错误");
			}
		}
		public static void AddCheckMode(string sCheckName, string sFunName, int CheckLength, string CheckCode, EnumCodeMode CodeMode = EnumCodeMode.None)
		{
			CheckMode TmpCheck = new CheckMode();
			TmpCheck.CheckName = sCheckName;
			TmpCheck.CheckFunName = sFunName;
			TmpCheck.CheckCode = CheckCode;
			TmpCheck.CheckLength = CheckLength;
			TmpCheck.CodeMode = CodeMode;
			CheckData.Add(TmpCheck);
		}
		public static void AddCheckMode(string sCheckName, string sFunName, int CheckLength, string CheckCode, string CheckDescription, EnumCodeMode CodeMode = EnumCodeMode.None)
		{
			CheckMode TmpCheck = new CheckMode();
			TmpCheck.CheckName = sCheckName;
			TmpCheck.CheckFunName = sFunName;
			TmpCheck.CheckCode = CheckCode;
			TmpCheck.CheckLength = CheckLength;
			TmpCheck.CheckDescription = CheckDescription;
			TmpCheck.CodeMode = CodeMode;
			CheckData.Add(TmpCheck);
		}
		public static string getTmpPath()
		{
			string tmp = System.Reflection.Assembly.GetExecutingAssembly().Location; //System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
			int a = tmp.LastIndexOf("\\");
			tmp = tmp.Substring(0, a);
			if (tmp.Substring(tmp.Length - 1, 1) != "\\")
			{
				tmp = tmp + "\\";
			}
			return tmp;
		}
		public static string getAddFileName(string FileName)
		{
			int a = 0;
			int c = 0;
			int b = 0;
			string noType = "";
			string TypeStr = "";
			string Value = "";
			string LeftP = "";
			a = FileName.LastIndexOf(".");
			c = FileName.LastIndexOf("\\");
			if (c < a)
			{
				noType = FileName.Substring(0, a);
				TypeStr = FileName.Substring(a);
			}
			else
			{
				noType = FileName;
				TypeStr = "";
			}
			if (noType.Substring(noType.Length - 1, 1) == ")")
			{
				b = Strings.InStrRev(noType, "(", -1, (Microsoft.VisualBasic.CompareMethod) 0);
				Value = noType.Substring(b + 1 - 1, noType.Length - b - 1);
				LeftP = noType.Substring(0, b - 1);
				if (Information.IsNumeric(Value))
				{
					Value = System.Convert.ToString(Conversion.Val(Value) + 1);
					return LeftP + "(" + Value + ")" + TypeStr;
				}
				else
				{
					return noType + "(1)" + TypeStr;
				}
			}
			else
			{
				return noType + "(1)" + TypeStr;
			}
		}
		public static string FormatTickCount(int Tim)
		{
			string returnValue = "";
			int ms = 0;
			int k = 0;
			int hm = 0;
			int se = 0;
			int ts = 0;
			k = Tim;
			hm = (int) ((double) k / 3600000);
			ms = (int) ((double) (k - 3600000 * hm) / 60000);
			se = (int) ((double) (k - 3600000 * hm - 60000 * ms) / 1000);
			ts = se;
			if (se < 0)
			{
				ts = 30 + se;
			}
			else
			{
				ts = 30 + se;
			}
			returnValue = Strings.Format(hm, "00") + ":" + Strings.Format(ms, "00") + ":" + Strings.Format(ts, "00");
			return returnValue;
		}
		/// <summary>
		/// 取函数或过程名，只对单个函数或过程进行取值，只支持Basic代码
		/// </summary>
		/// <param name="mCode">函数代码</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string getVbsSubOrFunName(string mCode)
		{
			//On Error Resume Next VBConversions Warning: On Error Resume Next not supported in C#
			string[] SaveCode = null;
			const string FunHead = "function ";
			const string SubHead = "sub ";
			string tmp = "";
			int F = 0;
			int S = 0;
			SaveCode = mCode.Split("\r\n");
			for (var i = 0; i <= SaveCode.Length - 1; i++)
			{
				tmp = SaveCode[(int) i];
				F = tmp.ToUpper().IndexOf(FunHead.ToUpper());
				S = tmp.ToUpper().IndexOf(SubHead.ToUpper());
				if (F >= 0 | S >= 0)
				{
					if (F >= 0)
					{
						int a = tmp.LastIndexOf("(");
						string TmpName = "";
						if (a > F)
						{
							if (a - F - FunHead.Length > 0)
							{
								TmpName = tmp.Substring(F + FunHead.Length, a - F - FunHead.Length);
								return TmpName.Trim();
							}
						}
					}
					if (S >= 0)
					{
						int a = tmp.ToUpper().LastIndexOf("(");
						string TmpName = "";
						if (a - S - SubHead.Length > 0)
						{
							TmpName = tmp.Substring(S + SubHead.Length, a - S - SubHead.Length);
							return TmpName.Trim();
						}
					}
				}
			}
			return "";
		}
		public static void ChangeButtonColor(gdButton obj)
		{
			gdButton with_1 = obj;
			with_1.AttachSkin = true;
			with_1.Radius = 2;
			with_1.DownBorder = Color.FromArgb(64, 0, 0);
			with_1.DownColor = new Color[] {Color.FromArgb(150, 150, 150), Color.FromArgb(180, 180, 180), Color.FromArgb(200, 200, 200), Color.FromArgb(230, 230, 230)};
			with_1.FocusBorder = Color.FromArgb(180, 180, 10);
			with_1.FocusColor = new Color[] {Color.FromArgb(250, 240, 201), Color.FromArgb(249, 238, 193), Color.FromArgb(251, 240, 177), Color.FromArgb(247, 237, 159), Color.FromArgb(247, 237, 139), Color.FromArgb(247, 237, 119)};
			with_1.NormalBorder = Color.FromArgb(180, 180, 180);
			with_1.NormalColor = new Color[] {Color.FromArgb(255, 247, 247, 247), Color.FromArgb(255, 237, 237, 237), Color.FromArgb(255, 217, 217, 217), Color.FromArgb(255, 207, 207, 207), Color.FromArgb(255, 217, 217, 217)}; //{Color.White, Color.FromArgb(240, 240, 240), Color.FromArgb(224, 224, 224), Color.White}
		}
		public static string GetControlType(Control Obj)
		{
			string TpName = Obj.GetType().ToString();
			return TpName;
		}
		public static string GetControlType(object Obj)
		{
			string TpName = Obj.GetType().ToString();
			return TpName;
		}
		
		public static void ChangeButtonFromFrm(object frm)
		{
			string tp = "";
			//On Error Resume Next VBConversions Warning: On Error Resume Next not supported in C#
			foreach (Control itemObj in frm.Controls)
			{
				tp = GetControlType(itemObj);
				if (tp == "喷码数据生成工具.gdButton")
				{
					ChangeButtonColor(itemObj);
				}
			}
			//MsgBox(tp)
		}
		public static string GetMac()
		{
			try
			{
				
				string stringMAC = "";
				string stringIP = "";
				
				ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
				ManagementObjectCollection queryCollection = query.Get();
				foreach (ManagementObject mo in queryCollection)
				{
					if (bool.Parse(mo["IPEnabled"].ToString()) == true)
					{
						stringMAC += System.Convert.ToString(mo["MACAddress"].ToString());
					}
				}
				return stringMAC;
			}
			catch (Exception)
			{
			}
			return string.Empty;
		}
		public static string CaculateRegistration(string MACAdress)
		{
			string Mac = MACAdress;
			string[] SplitMac = Mac.Split(':');
			string Result = "";
			for (var i = 0; i <= SplitMac.Length - 1; i++)
			{
				if (Result != "")
				{
					Result = Result + "-" + fenduanjisuan(SplitMac[(int) i], System.Convert.ToInt32(i));
				}
				else
				{
					Result = fenduanjisuan(SplitMac[(int) i], i);
				}
			}
			return Result;
		}
		private static string fenduanjisuan(string value, int sCount = 0)
		{
			if (value.Length != 2)
			{
				value = "0" + value;
			}
			string Str1 = "";
			string Str2 = "";
			int Str1Asc = 0;
			int Str2Asc = 0;
			string Tmp = "";
			int TmpAsc = 0;
			int Result1 = 0;
			int Result2 = 0;
			string Result = "";
			const string Code = "ssjj22020";
			try
			{
				Str1 = value.Substring(0, 1);
				Str2 = value.Substring(1, 1);
				Str1Asc = Strings.Asc(Str1);
				Str2Asc = Strings.Asc(Str2);
				Result1 = Str1Asc;
				Result2 = Str2Asc;
				for (var i = 0; i <= Code.Length - 1; i++)
				{
					Tmp = Code.Substring(System.Convert.ToInt32(i), 1);
					TmpAsc = Strings.Asc(Tmp);
					Result1 = Result1 ^ TmpAsc;
					Result2 = Result2 ^ TmpAsc;
				}
			}
			catch
			{
				Result1 = 13579;
				Result2 = 24681;
			}
			Result1 = Result1 * 6 + 43 + sCount * 1 * 3 * 5 * 7 * 9;
			Result2 = Result2 * 2 + 44 + sCount * 2 * 4 * 6 * 8 * 10;
			Result = System.Convert.ToString((int) (Conversion.Val(double.Parse(Result1.ToString() + Result2.ToString()) / 10))) % 100);
			if (Result.Length < 4)
			{
				Result = Strings.Format(Conversion.Val(Result) % 7, "00") + Strings.Format(Conversion.Val(Result) / 7, "00");
			}
			return Result;
		}
		public static void Registration(bool ShowInput = false)
		{
			string gMAC = GetMac();
			string CaculateRegionCode = "";
			string InputT = "";
			DES DesObj = new DES();
			if (gMAC.Length > 17)
			{
				gMAC = gMAC.Trim();
			}
			CaculateRegionCode = CaculateRegistration(gMAC);
			try
			{
				goto TrueLine;
				if (ShowInput)
				{
					goto RegisterLine;
				}
				if (string.IsNullOrEmpty(SaveData.RegisterCode))
				{
					SaveData.RegisterCode = ReadRegistryKey("CreateDataTool", "RegionCode"); //在本地文件中读取注册信息，无没有读取到从注册表读取注册码信息
				}
				// GoTo TrueLine
				if (string.IsNullOrEmpty(SaveData.RegisterCode))
				{
RegisterLine:
					// GoTo TrueLine
					InputT = RemedyCode.ShowInput(Form1.Default, "输入注册码", "", false);
					if (InputT == CaculateRegionCode)
					{
						RegionAccess = true;
						SaveData.RegisterCode = DesObj.Encrypt(InputT.Replace("-", ":"), "sj235689");
						try
						{
							WriteRegistryKey("CreateDataTool", "RegionCode", SaveData.RegisterCode);
						}
						catch
						{
						}
						Form1.Default.SaveSetting();
						Form1.Default.ChangeRegionObjEnabled();
						Interaction.MsgBox("注册成功！", MsgBoxStyle.Information, "提示");
					}
					else
					{
						RegionAccess = false;
						if (string.IsNullOrEmpty(InputT))
						{
							InputT = "ErrorCode";
						}
						SaveData.RegisterCode = InputT;
						Form1.Default.ChangeRegionObjEnabled();
						Form1.Default.SaveSetting();
						try
						{
							WriteRegistryKey("CreateDataTool", "RegionCode", SaveData.RegisterCode);
						}
						catch
						{
						}
						Interaction.MsgBox("错误的注册码，请输入正确的注册码！", MsgBoxStyle.Critical, "注册失败");
					}
				}
				else
				{
					string rT = SaveData.RegisterCode;
					if (CaculateRegionCode.ToUpper() == DesObj.Decrypt(SaveData.RegisterCode, "sj235689").Replace(":", "-"))
					{
TrueLine:
						RegionAccess = true;
						Form1.Default.ChangeRegionObjEnabled();
					}
					else
					{
						// GoTo RegisterLine
						RegionAccess = false;
						Form1.Default.ChangeRegionObjEnabled();
					}
				}
			}
			catch (Exception)
			{
				// MsgBox(ex.Message)
				RegionAccess = false;
				Form1.Default.ChangeRegionObjEnabled();
			}
		}
		public static void WriteRegistryKey(string NodeName, string ChildNodeName, string Value) //将注册信息写入注册表
		{
			RegistryKey Key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\" + NodeName, true);
			if (Key == null)
			{
				Key = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
				Key = Key.CreateSubKey(NodeName);
			}
			if (Key != null)
			{
				Key.SetValue(ChildNodeName, Value);
			}
		}
		public static string ReadRegistryKey(string NodeName, string ChildNodeName) //从注册表读取信息
		{
			RegistryKey Key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\" + NodeName, false);
			if (Key != null)
			{
				string Result = "";
				Result = Key.GetValue(ChildNodeName).ToString();
				return Result;
			}
			else
			{
				return "";
			}
		}
		public static string getOnlyPath(string FileName)
		{
			int a = FileName.LastIndexOf("\\");
			if (a >= 2)
			{
				return FileName.Substring(0, a) + "\\";
			}
			else
			{
				return "";
			}
		}
		/// <summary>
		/// 格式化数值为字符串，不足长度以0站位"
		/// </summary>
		/// <param name="value">欲格式化数据</param>
		/// <param name="length">数据长度</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static object FormatByLength(long value, int length)
		{
			const string TypeStr = "00000000000000000000000000000000000000000";
			if (TypeStr.Length < length)
			{
				throw (new Exception("所需数据长度超出软件限制！"));
			}
			string result = Strings.Format(value, TypeStr.Substring(0, length));
			return result;
		}
#region 创建快捷方式
		public static void CreateShotCut()
		{
			try
			{
				string DeskTop = GetDeskTop();
				string FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
				object wsh = Interaction.CreateObject("WScript.Shell", "");
				object Lnk = wsh.createshortcut(DeskTop + "\\喷码数据生成工具.lnk");
				Lnk.arguments = "/?";
				Lnk.description = "条形码喷码数据生成工具";
				Lnk.targetpath = FileName;
				Lnk.workingdirectory = Application.StartupPath;
				Lnk.windowstyle = 1;
				Lnk.save();
				Interaction.MsgBox("快捷方式创建成功！", MsgBoxStyle.Information, "提示");
			}
			catch (Exception ex)
			{
				Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "错误");
			}
		}
		/// <summary>
		/// 获取计算机用户名
		/// </summary>
		/// <returns></returns>
		public static string APIGetUserName()
		{
			string str1 = "";
			int a = 0;
			str1 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
			a = str1.LastIndexOf("\\");
			str1 = str1.Substring(0, a);
			return str1;
		}
		/// <summary>
		/// 获取计算机名
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string APIGetComputerName()
		{
			string str1 = "";
			int a = 0;
			str1 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
			a = str1.LastIndexOf("\\");
			str1 = str1.Substring(a + 1);
			return str1;
		}
		/// <summary>
		/// 获取程序所在路径
		/// </summary>
		/// <param name="FileName"></param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string getAppPath(string FileName)
		{
			int a = FileName.LastIndexOf("\\");
			string Result = "";
			if (a > 0)
			{
				Result = FileName.Substring(0, a);
				return Result;
			}
			else
			{
				return "";
			}
		}
		public static string getAppPath()
		{
			string FileName = System.Reflection.Assembly.GetExecutingAssembly().Location.ToString();
			int a = FileName.LastIndexOf("\\");
			string Result = "";
			if (a > 0)
			{
				Result = FileName.Substring(0, a);
				return Result;
			}
			else
			{
				return "";
			}
		}
		/// <summary>
		/// 获取程序Exe文件名
		/// </summary>
		/// <param name="FileName"></param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string getOnlyName(string FileName)
		{
			int a = FileName.LastIndexOf("\\");
			int b = FileName.LastIndexOf(".");
			string Result = "";
			if (b > a & a > 0 & b - a - 1)
			{
				Result = FileName.Substring(a + 1, b - a - 1);
				return Result;
			}
			else
			{
				return "";
			}
		}
		public static string getOnlyName()
		{
			string FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
			int a = FileName.LastIndexOf("\\");
			int b = FileName.LastIndexOf(".");
			string Result = "";
			if (b > a & a > 0 & b - a - 1)
			{
				Result = FileName.Substring(a + 1, b - a - 1);
				return Result;
			}
			else
			{
				return "";
			}
		}
		/// <summary>
		/// 获取文件名后缀类型
		/// </summary>
		/// <param name="FileName"></param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static string getOnlyType(string FileName)
		{
			int a = FileName.LastIndexOf(".");
			int b = FileName.LastIndexOf("\\");
			if (a > b & a > 0)
			{
				return FileName.Substring(a + 1).Trim();
			}
			else
			{
				return "";
			}
		}
		public static string GetDeskTop()
		{
			string tmp = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
			return tmp;
		}
		public static string GetStartMenuPath()
		{
			string tmp = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
			return tmp;
		}
#endregion
#region 查找替换内容翻译
		public static string TranslateText(string Value)
		{
			int a = 0;
			int b = 0;
			string tmp = Value;
			ArrayList dWord = new ArrayList();
			while (!(a < 0))
			{
				a = tmp.IndexOf("[");
				b = tmp.IndexOf("]");
				if (b > a & a >= 0)
				{
					string str1 = tmp.Substring(a, b - a + 1);
					dWord.Add(str1);
					tmp = tmp.Substring(b + 1);
				}
			}
			if (dWord.Count > 0)
			{
				return ModifySpacChar(Value, dWord);
			}
			else
			{
				return Value;
			}
		}
		private static string ModifySpacChar(string OldString, ArrayList dWord)
		{
			if (dWord != null)
			{
				if (dWord.Count > 0)
				{
					for (var i = 0; i <= dWord.Count - 1; i++)
					{
						string tmp = System.Convert.ToString(dWord[System.Convert.ToInt32(i)].ToString().Replace("[", "").Replace("]", ""));
						int a = tmp.IndexOf(":");
						string Str1;
						string Str2 = "";
						if (a > 0)
						{
							Str1 = tmp.Substring(0, a);
							Str2 = tmp.Substring(a + 1);
							if (Str1.ToLower() == "chr" && Information.IsNumeric(Str2))
							{
								OldString = OldString.Replace(dWord[System.Convert.ToInt32(i)].ToString(), ValueToChar((int) (Conversion.Val(Str2))));
							}
						}
					}
					return OldString;
				}
				else
				{
					return OldString;
				}
			}
			else
			{
				return OldString;
			}
		}
		public static char ValueToChar(int value)
		{
			try
			{
				byte[] a = new byte[] {value};
				char Result = (char) 0;
				Result = System.Text.Encoding.GetEncoding("IBM437").GetChars(a)[0];
				return Result;
			}
			catch
			{
				return "";
			}
		}
		public static string getItemValue(ListViewItem Item, int Index)
		{
			if (Item != null)
			{
				if (Index >= 0 & Index < Item.SubItems.Count)
				{
					return Item.SubItems[Index].Text;
				}
				else
				{
					return "";
				}
			}
			else
			{
				return "";
			}
		}
		public static void SetItemValue(ListViewItem Item, int Index, string Value)
		{
			if (Item != null)
			{
				if (Index >= 0 & Index < Item.SubItems.Count)
				{
					Item.SubItems[Index].Text = Value;
				}
				else
				{
					for (var i = Item.SubItems.Count; i <= Index; i++)
					{
						Item.SubItems.Add("");
					}
					Item.SubItems[Index].Text = Value;
				}
			}
		}
		public static ColorBlend getColorBlendByColors(Color[] Value)
		{
			try
			{
				ColorBlend Result = new ColorBlend();
				float[] p = null;
				if (Value == null)
				{
					return null;
				}
				if (Value.Length <= 0)
				{
					return null;
				}
				for (var i = 0; i <= Value.Length - 1; i++)
				{
					Array.Resize(ref p, (int) i + 1);
					p[(int) i] = System.Convert.ToSingle((double) i / (Value.Length - 1));
				}
				Result.Colors = Value;
				Result.Positions = p;
				return Result;
			}
			catch
			{
				return null;
			}
		}
#endregion
	}
	
}
